package com.goldenautumn.prefabricate.business.user

import android.provider.ContactsContract.CommonDataKinds.Nickname
import com.goldenautumn.prefabricate.business.entity.AddressCreateParams
import com.goldenautumn.prefabricate.business.entity.AddressDetailsResp
import com.goldenautumn.prefabricate.business.entity.AddressInfoVo
import com.goldenautumn.prefabricate.business.entity.AddressResp
import com.goldenautumn.prefabricate.business.entity.ImageModelResp
import com.goldenautumn.prefabricate.business.entity.LanguageInfo
import com.goldenautumn.prefabricate.business.entity.LoginParams
import com.goldenautumn.prefabricate.business.entity.LoginReqResult
import com.goldenautumn.prefabricate.business.entity.PaymentMethodVo
import com.goldenautumn.prefabricate.business.entity.RegionTreeVo
import com.goldenautumn.prefabricate.business.entity.UserInfoReqResult
import com.goldenautumn.prefabricate.business.service.UserService
import com.goldenautumn.prefabricate.utils.getToken
import com.goldenautumn.prefabricate.utils.objectToMap
import okhttp3.MultipartBody
import qncx.external.common.network.SimpleRepository
import qncx.external.common.room.table.UserInfo

class UserRepositoryImpl : SimpleRepository() {

    suspend fun onLogin(source: LoginParams): LoginReqResult {
        var map = HashMap<String, Any>()
        map["s"] = "api/user/mobileLogin"
        map["token"] = getToken()
        map["source"] = "20"
        map["token_type"] = "andriod"
        map["mobile"] = "${source.mobile}"
        map["code"] = "${source.code}"
        return getService(UserService::class.java).onLogin(map,map)
    }

    suspend fun onWeChatLogin(source: String): LoginReqResult {
        var map = HashMap<String, Any>()
        map["s"] = "api/user/appLogin"
        map["code"] = source
        return getService(UserService::class.java).onWeChatLogin(map)
    }

    suspend fun getCode(source: String,type:String? = "20"): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/user/sendSms"
        map["mobile"] = source
        map["scene"] = "$type"
        return getService(UserService::class.java).onSend(map,map)
    }

    suspend fun getUserInfo(): UserInfoReqResult {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.index/detail"
        map["token"] = getToken()
        return getService(UserService::class.java).getUserInfo(map)
    }

    suspend fun getRegionTree(): List<RegionTreeVo> {
        var map = HashMap<String, Any>()
        map["s"] = "api/address/getRegionTree"
        map["token"] = getToken()
        return getService(UserService::class.java).getRegionTree(map)
    }

    suspend fun getAddressList(): AddressResp {
        var map = HashMap<String, Any>()
        map["s"] = "api/address/lists"
        map["token"] = getToken()
        return getService(UserService::class.java).getAddressList(map)
    }

    suspend fun createAddress(params: AddressCreateParams): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/address/add"
        map["token"] = getToken()
        if (params.address_id != null) map["address_id"] = "${params.address_id}"
        var map1 = HashMap<String, Any>()
        map1["name"] = "${params.name}"
        map1["phone"] = "${params.phone}"
        map1["region"] = "${params.region}"
        map1["detail"] = "${params.detail}"
        map1["is_default"] = "${params.is_default}"
        return getService(UserService::class.java).onCreateAddress(map, map1)
    }

    suspend fun updateAddress(params: AddressCreateParams): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/address/edit"
        map["token"] = getToken()
        if (params.address_id != null) map["address_id"] = "${params.address_id}"
        var map1 = HashMap<String, Any>()
        map1["name"] = "${params.name}"
        map1["phone"] = "${params.phone}"
        map1["region"] = "${params.region}"
        map1["detail"] = "${params.detail}"
        map1["is_default"] = "${params.is_default}"
        return getService(UserService::class.java).onUpdateAddress(map, map1)
    }

    suspend fun deleteAddress(id: String): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/address/delete"
        map["token"] = getToken()
        map["address_id"] = "$id"
        return getService(UserService::class.java).onDeleteAddress(map)
    }

    suspend fun defaultAddress(id: String): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/address/setDefault"
        map["token"] = getToken()
        map["address_id"] = "$id"
        return getService(UserService::class.java).onDefaultAddress(map)
    }

    suspend fun addressDetails(id: String): AddressDetailsResp {
        var map = HashMap<String, Any>()
        map["s"] = "api/address/detail"
        map["token"] = getToken()
        map["address_id"] = "$id"
        return getService(UserService::class.java).onAddressDetails(map)
    }

    suspend fun onPayType(): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/order/payType"
        map["token"] = getToken()
        return getService(UserService::class.java).payType(map)
    }

    suspend fun updateImage(img: MultipartBody.Part): ImageModelResp {
        var map = HashMap<String, Any>()
        map["s"] = "api/upload/image"
        map["token"] = getToken()
        return getService(UserService::class.java).updateImage(map,img)
    }

    suspend fun updateInfo(nickname: String? = null,avatar: String? = null): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.index/update"
        map["token"] = getToken()
        var user = com.goldenautumn.prefabricate.utils.getUserInfo()
        var map1 = HashMap<String, Any>()
        map1["nickName"] = nickname ?: "${user?.nickName}"
        map1["avatarUrl"] = avatar ?: "${user?.avatarUrl}"
        return getService(UserService::class.java).updateInfo(map,map1)
    }

    suspend fun bindPhone(phone: String,code: String): Any {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.index/bindMobile"
        map["token"] = getToken()

        var map1 = HashMap<String, Any>()
        map1["mobile"] = "$phone"
        map1["code"] = "$code"

        return getService(UserService::class.java).bindPhone(map,map1)
    }

    suspend fun bindWechat(code: String): LoginReqResult {
        var map = HashMap<String, Any>()
        map["s"] = "api/user.index/bindWx"
        map["token"] = getToken()
        map["code"] = code
        return getService(UserService::class.java).bindWechat(map)
    }

    suspend fun getLanguage(): List<LanguageInfo> {
        var map = HashMap<String, Any>()
        map["s"] = "api/page/language"
        return getService(UserService::class.java).getLanguage(map)
    }

}